import ApiRadio from './ApiRadio';


export default class ApiRadioGroup {
    constructor(element, option) {
        this.$element = $(element, option);
        this.$radios = this.$element.find('label.api-radio');
        this.options = Object.assign({}, option);
        this.apiRadios = [];
        this.init();
    }

    get value() {
        let validateValue;
        this.apiRadios.forEach(function (item) {
            let value = item.radioValue;
            if (value != null) {
                validateValue = value;
            }
        })
        return validateValue;
    }

    init() {
        this.$radios.each((index, item) => {
            this.apiRadios.push(new ApiRadio(item))
        });

        this.$radios.find('input[type="radio"]').on('change', (e) => {
            const $apiRadio = $(e.target).parents('.api-radio')
            let label = $apiRadio.data('label');
            label = label ? parseInt(label) : false;
            this.options.change && this.options.change(this.value, label, $apiRadio, this.$element)
        })
    }
}